package com.amkai.aes.webservice.binary.objecthandler.inventory;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;

import com.amkai.aes.aescommon.service.binary.job.inventory.BEFacilitiesAddressJob;
import com.amkai.aes.generatedbeans.common.Address;
import com.amkai.aes.generatedbeans.common.AddressKind;
import com.amkai.aes.generatedbeans.organization.BaseOrganizationAddressEntry;
import com.amkai.aes.webservice.binary.JobObjectHandler;
import com.amkai.common.extensions.hibernate.config.SiteManager;
import com.amkai.common.extensions.hibernate.config.manager.SessionManager;

public class BEFacilitiesAddressHandler implements JobObjectHandler<BEFacilitiesAddressJob,Serializable, ArrayList<Address>> {

	// KKORNEL: AES-17493
//	private static final String query = "select bae.address from BaseOrganizationAddressEntry bae where bae.baseOrganization.id = :id and bae.address.kindWrapper = :kind";
//	private static final String allfacaddresses = "select bae.address from BaseOrganizationAddressEntry bae where bae.baseOrganization.id in (select fac.id from Facility fac where fac.parent.id = :id)  and bae.address.kindWrapper = :kind";
	private static final String query = "select bae.address from BaseOrganizationAddressEntry bae where bae.baseOrganization.id = :id and (bae.address.kindWrapper.id in (:kinds))";
	private static final String allfacaddresses = "select bae.address from BaseOrganizationAddressEntry bae where bae.baseOrganization.id in (select fac.id from Facility fac where fac.parent.id = :id)  and (bae.address.kindWrapper.id in (:kinds))";

	
	@Override
	public ArrayList<Address> process(BEFacilitiesAddressJob job,
			Serializable object) throws Exception {
		SessionManager sm = SiteManager.Start();
		try {
			List<Address> addresses = new ArrayList<Address>();
			// KKORNEL: AES-17493
			List<Integer> kindIds = new ArrayList<Integer>();
			for (AddressKind addressKind : job.akids) {
				kindIds.add(addressKind.ordinal());
			}
			ArrayList<Address> result = new ArrayList<Address>();
 			sm.openSession();
 			Query q;
 			if (job.needAllFacilitiesAddress)
 				q = sm.execute(allfacaddresses);
 			else
 				 q = sm.execute(query);
			
			q = q.setLong("id", job.baseOrgId);
			// KKORNEL: AES-17493
//			q = q.setLong("kind", job.akid.ordinal());
			q = q.setParameterList("kinds", kindIds);
			addresses = q.list();
			for (Address a : addresses){
//				if (a.getKind().equals(job.akid)){
				if (kindIds.contains(a.getKind().ordinal())){
					a.getShip_purchaseOrders().size();
					a.getBill_purchaseOrders().size();
					if (a.getLocation() != null) {
						if (a.getLocation().getPhone() != null) {
							a.getLocation().getPhone().getNumber();
						}
						if (a.getLocation().getFax() != null) {
							a.getLocation().getFax().getNumber();
						}
						if (a.getLocation().getAlterPhone() != null) {
							a.getLocation().getAlterPhone()
									.getNumber();
						}
						if (a.getLocation().getZipCode() != null) {
							if (a.getLocation().getZipCode()
									.getCity() != null) {
								a.getLocation().getZipCode()
										.getCity().getName();
							}
							if (a.getLocation().getZipCode()
									.getZip() != null) {
								a.getLocation().getZipCode()
										.getZip().getName();

								if (a.getLocation().getZipCode()
										.getZip().getState() != null) {
									a.getLocation().getZipCode()
											.getZip().getState()
											.getName();
								}
							}
						}
						
					}

					if (a.getBaseOrganizationAddressEntries() != null){
						for (BaseOrganizationAddressEntry be : a.getBaseOrganizationAddressEntries()){
							be.getBaseOrganization().getId();
							be.getBaseOrganization().getName();
							be.getAddress().getId();
							be.getAddress().getActive();
							if (be.getAddress().getLocation() != null)
								be.getAddress().getLocation().getId();
						}
					}
					result.add(a);
				}
			}

		return result;
		} catch (Exception e) {
			// logger.error("ImportCheckoutHandler: ", e);

			throw e;
		} finally {
			sm.closeSession();
		}

	}

}
